/*
  Copyright (C) 2023 mio <stigma@disroot.org>

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/* ar.h
 *
 * This file contains the definitions for extracting archive
 * files.  The GUI/CLI (The Interface) should call these functions to handle
 * extracting archive files.
 *
 * The Interface should call:
 *  - ar_init_extractor to initialize the extractor and it's resources.
 *  - ar_decompress_archive to extract the archive
 *  - ar_check_complete to check if we've finished extracting the archive.
 *  - ar_end_extracting to release resources used by the extractor.
 *
 * NOTE: No threading is handled in these functions, it is up to The Interface
 *       to implement threading.
 *
 * An example of supporting threading with pthread would be:
 *
 *    AR *extractor;
 *    pthread_t pid;
 *
 *    extractor = ar_init_extractor("file.tar", NULL);
 *    pthread_create(&pid, NULL, &ar_decompress_archive, extractor);
 *    pthread_detach(pid);
 *
 *    while(false == ar_check_complete(extractor)) {
 *        ...
 *    }
 *    ar_end_extracting(extractor);
 */

/* Opaque data type. */
typedef struct AR AR;

/* Initialize the core extractor.
 *
 * This involves setting up the charset detector and converter,
 * the archive reader and writer.
 *
 * @param src The source archive file to read from.
 * @param destdir The destination director the archive will be
 * extracted into.
 * @param two_pass Perform two passes of the archive to determine
 * if a parent directory needs to be created.
 */
AR *ar_init_extractor(const char *src, const char *destdir, const AROptions *options);

/* Release resources for the archive extractor. */
bool ar_end_extracting(AR *ar);

/* Check if the archive has been extracted.
 *
 * NOTE: This will return true even if there was an error. There is no error
 * handling provided yet.
 */
bool ar_check_complete(AR *ar);

/* Decompress the archive */
void *ar_decompress_archive(AR *ar);
